﻿using System;
using System.Collections.Generic;
using System.Text;
using XDesigner.AppFramework;
using XDesigner.AppFramework.WinForms;
using 刘颜红销售项目管理.Business.DOM;
using 刘颜红销售项目管理.Business;
using XDesigner.ORM;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Design;
using System.Collections;

namespace 刘颜红销售项目管理
{
    public class StructureTreeView : System.Windows.Forms.TreeView
    {

        private DomTreeViewControler myTreeControl = new DomTreeViewControler();

      

        public StructureTreeView()
        {
            this.HideSelection = false;
            this.ImageList = new System.Windows.Forms.ImageList();

            myTreeControl.TreeView = this ;
            myTreeControl.SupportDynamicLoad = true ;

            MyApplicationFramework.Instance.ORMEngine.EventSender.AddListener(
                this, 
                new ORMEventHandler(this.HandleORMEvent));
        }

        private bool _ShowActionNode = false;
        [System.ComponentModel.DefaultValue(false)]
        public bool ShowActionNode
        {
            get
            {
                return _ShowActionNode;
            }
            set
            {
                _ShowActionNode = value;
            }
        } 

        protected override void Dispose(bool disposing)
        {
            MyApplicationFramework.Instance.ORMEngine.EventSender.RemoveListener(this);
            base.Dispose(disposing);
        }

        private void HandleORMEvent(object sender, ORMEventArgs args)
        {
            if (this.IsDisposed || this.IsHandleCreated == false)
            {
                return;
            }
            if (this.DesignMode)
            {
                return;
            }

            switch (args.Style)
            {
                case ORMEventStyle.InsertInstance:
                    if (args.Instance is Class工作单位 || args.Instance is Class人员基本信息)
                    {
                        myTreeControl.RefreshChildNodes( this.SelectedNode, false);
                    }
                    break;
                case ORMEventStyle.DeleteInstance:
                    if (args.Instance is Class工作单位 || args.Instance is Class人员基本信息)
                    {
                        myTreeControl.RefreshNode( this.SelectedNode, true);
                    }
                    break;
                case ORMEventStyle.UpdateInstance:
                    if (args.Instance is Class工作单位 || args.Instance is Class人员基本信息)
                    {
                        TreeNode node = myTreeControl.GetNodeFast(args.Instance);
                        if (args.Instance is Class人员基本信息)
                        {
                            Class人员基本信息 p = (Class人员基本信息)args.Instance;
                            if ( node.Parent != null && node.Parent.Tag != p.所属单位)
                            {
                                myTreeControl.ChangeParent(p, p.所属单位);
                            }
                        }
                        if (node != null)
                        {
                            myTreeControl.RefreshSingleNode(node);
                        }
                    }
                    break;
            }//switch
        }

        public void RefreshView()
        {
            this.BeginUpdate();
            myTreeControl.RootInstance = MyApplicationFramework.Instance;
            StructureTreeProvider provider = new StructureTreeProvider();
            provider.ShowActionNode = this.ShowActionNode;
            myTreeControl.Provider = provider;
            myTreeControl.RefreshView();

            myTreeControl.Nodes[0].Expand();
            myTreeControl.SelectedNode = this.Nodes[0];
            this.EndUpdate();
        }

        public DomTreeViewControler Controler
        {
            get
            {
                return myTreeControl;
            }
        }

        public object SelectedValue
        {
            get
            {
                return myTreeControl.SelectedValue;
            }
            set
            {
                myTreeControl.SelectedValue = value;
            }
        }
    }
}
